package com.woniuxy.shiro;

import com.woniuxy.entity.Admin;
import com.woniuxy.service.AdminService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import javax.annotation.Resource;

public class AdminRealm extends AuthorizingRealm {
    @Resource
    private AdminService adminService;
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        try {
            Admin admin = adminService.selectByNameOrTel((String) token.getPrincipal());
            if (admin != null) {
                SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(admin,admin.getPassword(), getName());
                return info;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
